<?php 

	define('SVN_ROOT_URL','http://ebuildy.googlecode.com/svn/trunk/ebuildy/');
	define('DOCUMENT_ROOT',dirname(__FILE__).'/test/');

	$buffer = array('ebuildy/cache/webConfiguration.inc.php',
							'ebuildy/class/EbApplication.php',
							'ebuildy/class/EbController.php',
							'ebuildy/class/EbDataHolder.php',
							'ebuildy/class/EbDebug.php',
							'ebuildy/class/EbFilesToolkit.php',
							'ebuildy/class/EbHtmlPage.php',
							'ebuildy/class/EbHTMLToolkit.php',
							'ebuildy/class/EbModel.php',
							'ebuildy/class/EbModule.php',
							'ebuildy/class/EbMySQLDataSource.php',
							'ebuildy/class/EbRequest.php',
							'ebuildy/class/EbResponse.php',
							'ebuildy/class/EbSession.php',
							'ebuildy/class/EbSQLModel.php',
							'ebuildy/class/EbSVNUpdate.php',
							'ebuildy/class/EbTableColumn.php',
							'ebuildy/class/EbTableSQLModel.php',
							'ebuildy/class/EbTemplate.php',
							'ebuildy/class/EbToolkit.php',
							'ebuildy/class/EbUrlRoutingToolkit.php',
							'ebuildy/class/EbView.php',
							'ebuildy/class/EbWidget.php',
							'ebuildy/class/EbXMLToolkit.php',
							'ebuildy/class/Event.php',
							'ebuildy/functions.inc.php',
							'ebuildy/gateway/AmfGateway.php',
							'ebuildy/interface/ICommand.php',
							'ebuildy/interface/ICRUDModel.php',
							'ebuildy/interface/IDataBaseTable.php',
							'ebuildy/interface/IDataSource.php',
							'ebuildy/interface/IEvent.php',
							'ebuildy/interface/IFilter.php',
							'ebuildy/interface/IModel.php',
							'ebuildy/lib/amfphp/.htaccess',
							'ebuildy/lib/amfphp/core/amf/app/Actions.php',
							'ebuildy/lib/amfphp/core/amf/app/Filters.php',
							'ebuildy/lib/amfphp/core/amf/app/Gateway.php',
							'ebuildy/lib/amfphp/core/amf/io/AMFBaseDeserializer.php',
							'ebuildy/lib/amfphp/core/amf/io/AMFBaseSerializer.php',
							'ebuildy/lib/amfphp/core/amf/io/AMFDeserializer.php',
							'ebuildy/lib/amfphp/core/amf/io/AMFSerializer.php',
							'ebuildy/lib/amfphp/core/amf/readme.txt',
							'ebuildy/lib/amfphp/core/amf/util/AMFObject.php',
							'ebuildy/lib/amfphp/core/amf/util/DateWrapper.php',
							'ebuildy/lib/amfphp/core/amf/util/DescribeService.php',
							'ebuildy/lib/amfphp/core/amf/util/PageAbleResult.php',
							'ebuildy/lib/amfphp/core/amf/util/TraceHeader.php',
							'ebuildy/lib/amfphp/core/amf/util/WrapperClasses.php',
							'ebuildy/lib/amfphp/core/json/app/Actions.php',
							'ebuildy/lib/amfphp/core/json/app/Gateway.php',
							'ebuildy/lib/amfphp/core/shared/adapters/adodbAdapter.php',
							'ebuildy/lib/amfphp/core/shared/adapters/doctrineAdapter.php',
							'ebuildy/lib/amfphp/core/shared/adapters/fbsqlAdapter.php',
							'ebuildy/lib/amfphp/core/shared/adapters/informixAdapter.php',
							'ebuildy/lib/amfphp/core/shared/adapters/mssqlAdapter.php',
							'ebuildy/lib/amfphp/core/shared/adapters/mysqlAdapter.php',
							'ebuildy/lib/amfphp/core/shared/adapters/mysqliAdapter.php',
							'ebuildy/lib/amfphp/core/shared/adapters/mysqliobjectAdapter.php',
							'ebuildy/lib/amfphp/core/shared/adapters/oci8Adapter.php',
							'ebuildy/lib/amfphp/core/shared/adapters/odbcAdapter.php',
							'ebuildy/lib/amfphp/core/shared/adapters/pdoAdapter.php',
							'ebuildy/lib/amfphp/core/shared/adapters/peardbAdapter.php',
							'ebuildy/lib/amfphp/core/shared/adapters/pgsqlAdapter.php',
							'ebuildy/lib/amfphp/core/shared/adapters/plainrecordsetAdapter.php',
							'ebuildy/lib/amfphp/core/shared/adapters/RecordSetAdapter.php',
							'ebuildy/lib/amfphp/core/shared/adapters/sqliteAdapter.php',
							'ebuildy/lib/amfphp/core/shared/adapters/sqliteobjectAdapter.php',
							'ebuildy/lib/amfphp/core/shared/adapters/zendrowAdapter.php',
							'ebuildy/lib/amfphp/core/shared/adapters/zendrowsetAdapter.php',
							'ebuildy/lib/amfphp/core/shared/app/BasicActions.php',
							'ebuildy/lib/amfphp/core/shared/app/BasicGateway.php',
							'ebuildy/lib/amfphp/core/shared/app/Constants.php',
							'ebuildy/lib/amfphp/core/shared/app/Globals.php',
							'ebuildy/lib/amfphp/core/shared/app/php4Executive.php',
							'ebuildy/lib/amfphp/core/shared/app/php5Executive.php',
							'ebuildy/lib/amfphp/core/shared/exception/MessageException.php',
							'ebuildy/lib/amfphp/core/shared/exception/php4Exception.php',
							'ebuildy/lib/amfphp/core/shared/exception/php5Exception.php',
							'ebuildy/lib/amfphp/core/shared/util/Authenticate.php',
							'ebuildy/lib/amfphp/core/shared/util/CharsetHandler.php',
							'ebuildy/lib/amfphp/core/shared/util/CompatPhp4.php',
							'ebuildy/lib/amfphp/core/shared/util/CompatPhp5.php',
							'ebuildy/lib/amfphp/core/shared/util/functions.php',
							'ebuildy/lib/amfphp/core/shared/util/Headers.php',
							'ebuildy/lib/amfphp/core/shared/util/JSON.php',
							'ebuildy/lib/amfphp/core/shared/util/MessageBody.php',
							'ebuildy/lib/amfphp/core/shared/util/MessageHeader.php',
							'ebuildy/lib/amfphp/core/shared/util/MethodTable.php',
							'ebuildy/lib/amfphp/core/shared/util/NetDebug.php',
							'ebuildy/lib/amfphp/core/xmlrpc/app/Actions.php',
							'ebuildy/lib/amfphp/core/xmlrpc/app/Gateway.php',
							'ebuildy/lib/amfphp/json.php',
							'ebuildy/lib/amfphp/phpinfo.php',
							'ebuildy/lib/amfphp/services/amfphp/DiscoveryService.php',
							'ebuildy/lib/amfphp/services/place_services_here.txt',
							'ebuildy/lib/amfphp/xmlrpc.php',
							'ebuildy/lib/phpsvnclient/definitions.php',
							'ebuildy/lib/phpsvnclient/http.php',
							'ebuildy/lib/phpsvnclient/PhpSVNClient.php',
							'ebuildy/lib/phpsvnclient/xml2array.php',
							'ebuildy/lib/phpsvnclient/xml_parser.php',
							'ebuildy/lib/Serializer.php',
							'ebuildy/lib/Unserializer.php',
							'ebuildy/lib/XML/Util.php',
							'ebuildy/static/eb_buttons.css',
							'ebuildy/static/eb_datagrid.css',
							'ebuildy/static/eb_misc.css',
							'ebuildy/static/images/add.gif',
							'ebuildy/static/images/edit.png',
							'ebuildy/static/images/headerlistingbg.png',
							'ebuildy/static/images/supprime.png',
							'ebuildy/static/images/view.png',
							'ebuildy/static/images/wiiboutons.png',
							'ebuildy/static/images/wiiminiboutons.png',
							'ebuildy/static/js/eb-live-editor.js',
							'ebuildy/static/js/jq/jquery.animated.innerfade.js',
							'ebuildy/static/js/jq/jquery.bookmark.js',
							'ebuildy/static/js/jq/jquery.cross-slide.js',
							'ebuildy/static/js/jq/jquery.easing.js',
							'ebuildy/static/js/jq/jquery.floatingbox.js',
							'ebuildy/static/js/jq/jquery.floatobject.js',
							'ebuildy/static/js/jq/jquery.galleria.js',
							'ebuildy/static/js/jq/jquery.innerfade.js',
							'ebuildy/static/js/jq/jquery.min.js',
							'ebuildy/static/js/jq/jquery.prettyPhoto.js',
							'ebuildy/static/js/jq/jquery.scrollfollow.js',
							'ebuildy/static/js/jq/jquery.uicore.js',
							'ebuildy/static/js/swfobject.js',
							'ebuildy/template/applicationStartTemplate.php',
							'ebuildy/template/command.txt',
							'ebuildy/template/DebugTemplate.php',
							'ebuildy/template/ErrorTemplate.php',
							'ebuildy/template/filter.txt',
							'ebuildy/template/model.txt',
							'ebuildy/template/table.txt',
							'ebuildy/template/vo.txt',
							'ebuildy/widget/ComponentWidget.php',
							'ebuildy/widget/DatagridWidget.php',
							'ebuildy/widget/FlashimagerotatorWidget.php',
							'ebuildy/widget/FlashWidget.php',
							'ebuildy/widget/FormWidget.php',
							'ebuildy/widget/ListWidget.php',
							'ebuildy/widget/ViewStackWidget.php');
	
	$listFilesToUpload = array();
	
	foreach ($buffer AS $item)
	{
		if (!file_exists(DOCUMENT_ROOT.$item))
		{
			$listFilesToUpload []= $item;
		}
	}
		
	function parse_folder($folder)
	{
		$url = SVN_ROOT_URL.$folder;
		
		$buffer = file_get_contents($url);
		
		$a = strpos($buffer,'<ul>');
		$b = strpos($buffer,'</ul>');
		
		$buffer = substr($buffer,$a,$b - $a);
		
		$buffer = str_replace(array('<li>','</li>'),array('',''),$buffer);
		$buffer = explode("\n",$buffer);
		
		$result = array();
		
		foreach ($buffer AS $item)
		{
			$a = strpos($item,'>');
			$b = strpos($item,'</');
		
			$item = substr($item,$a + 1,$b - $a - 1);
			
			if (is_string($item) && $item != '.' && $item != '..')
			{
				$result []= $item;
			}
		}
		
		
		return $result;
	}
	
	function upload_folder($folder)
	{
		$buffer = parse_folder($folder);
		
		echo '<ul><b>Upload folder '.$folder.'</b>';
		
		foreach ($buffer AS $item)
		{
			if (strpos($item,'.') === false)
			{
				if (!file_exists(DOCUMENT_ROOT.$folder.$item))
				{
					echo '<li>mkdir '.$folder.$item.'</li>';
					
					if (@mkdir(DOCUMENT_ROOT.$folder.$item,0777,true) === false)
					{
						continue;
					}
				}
				
				upload_folder($folder.$item);
			}
			else
			{
				//upload_file($folder.$item);
			}
		}
		
		echo '</ul>';
		
		flush();
	}

	function upload_file($file)
	{
		$url = SVN_ROOT_URL.$file;
		$folder = dirname($file);
			
		if (!file_exists(DOCUMENT_ROOT.$folder))
		{
			if (@mkdir(DOCUMENT_ROOT.$folder.$item,0777,true) === false)
			{
				return false;
			}
		}
		
		$buffer = file_get_contents($url);
		
		return file_put_contents(DOCUMENT_ROOT.$file,$buffer);
	}
	
	
	if (isset($_GET['file']))
	{
		$file = $_GET['file'];
		
		$result = upload_file($file);

		$index = 0;

		foreach ($listFilesToUpload AS $item)
		{
			if ($file === $item)
			{
				break;
			}
			
			$index ++;
		}
		
		$index ++;
		
		if ($index < count($listFilesToUpload))
		{
			die(json_encode((object) array('next' => $listFilesToUpload[$index],'perc' => 100*$index / count($listFilesToUpload), 'result' => $result)));
		}
		else
		{
			die(json_encode((object) array('next' => 'end','perc' => 100)));
		}
	}
	
//	include "ebuildy/class/EbFilesToolkit.php";
//
//	$buffer = EbFilesToolkit::getFiles(dirname(__FILE__).'/ebuildy','//');
//	
//	foreach ($buffer AS &$item)
//	{
//		if (strpos($item,'.svn') === false)
//		{
//			$item = substr($item,strlen('C:/Program Files/EasyPHP 3.0/www/ebuildy/'));
//		
//			echo "'$item',<br/>";
//		}
//	}
//	
//	die();
?>
<!DOCTYPE html>
<html>
<head>
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
	
	<?php if (count($listFilesToUpload)): ?>
	<script type="text/javascript">

		(function($) {	
			$.fn.reportprogress = function(val,maxVal) {			
				var max=100;
				if(maxVal)
					max=maxVal;
				return this.each(
					function(){		
						var div=$(this);
						var innerdiv=div.find(".progress");
						
						if(innerdiv.length!=1){						
							innerdiv=$("<div class='progress'></div>");					
							div.append("<div class='text'>&nbsp;</div>");
							$("<span class='text'>&nbsp;</span>").css("width",div.width()).appendTo(innerdiv);					
							div.append(innerdiv);					
						}
						var width=Math.round(val/max*100);
						innerdiv.css("width",width+"%");	
						div.find(".text").html(width+" %");
					}
				);
			};
		})(jQuery);	

		function upload(file)
		{
			$('#console').html('- Uploading ' + file + '...<br />' + $('#console').html());

			$.getJSON('/ebuildy_loader.php?file=' + file,
					function (data)
					{
						$("#progressbar").reportprogress(data.perc);
				
						if(data.next != 'end')
						{
							upload(data.next);
						}
					});
		}

		$(function ()
		{
			$("#progressbar").reportprogress(0);
		});
	
	</script>
	
	<style>
		/* progress bar container */
		#progressbar{
		        border:1px solid black;
		        width:200px;
		        height:20px;
		        position:relative;
		        color:black; 
		        margin:auto;
		}
		/* color bar */
		#progressbar div.progress{
		        position:absolute;
		        width:0;
		        height:100%;
		        overflow:hidden;
		        background-color:#369;
		}
		/* text on bar */
		#progressbar div.progress .text{
		        position:absolute;
		        text-align:center;
		        color:white;
		}
		/* text off bar */
		#progressbar div.text{
		        position:absolute;
		        width:100%;
		        height:100%;
		        text-align:center;
		}
	</style>
	<?php endif; ?>
</head>
<body>
	<?php if (count($listFilesToUpload) != 0): ?>
	<a href="Upload" onclick="upload('<?php echo $listFilesToUpload[0]; ?>');return false;">Upload eBuildy!</a>
	
	<div id="progressbar"></div>
	<br />
	<div id="console" style="width:800px;height:200px;margin:auto;border:1px solid #000000;background:#FFFFFF;padding:5px;overflow:scroll;">

	</div>
	<?php else: ?>
	
	<h2>eBuildy is 100% installed!</h2>
	
	<?php endif; ?>
</body>

</html>
